/*
 * fcaGroup.java
 *
 * Created on 30. prosinec 2004, 21:21
 */

package cz.masa.FotoCAT;

import java.sql.*;
import java.util.*;
/**
 *
 * @author Jiri Masnik
 */
public class fcaGroup {
    private int     db_id;
    private String  db_name;
    
    private static final String MAIN_SQL_FLDS = "fca01.fca01_id, fca01.fca01_fca01_id, fca01.fca01_name";
    public static String getMainSQLFlds(){ return fcaGroup.MAIN_SQL_FLDS; }
    private static final String MAIN_SQL = "SELECT " + fcaGroup.MAIN_SQL_FLDS + " FROM fca01_group AS fca01";
    public static String getMainSQL(){ return fcaGroup.MAIN_SQL; }
    
    /** Creates a new instance of fcaGroup */
    public fcaGroup() {
        this.db_id = -1;
    }
    
    public int dbLoad(ResultSet rs){
        try {
            this.db_id = rs.getInt("fca01.fca01_id");
            this.db_name = rs.getString("fca01.fca01_name");
            return 1;
        }
        catch(SQLException ex) {
            return 0;
        }
    }
    
    public int getId(){
        return this.db_id;
    }
    
    public String getName(){
        return this.db_name;
    }
    
    public int rename(String newname){
        SQLCon.exSQL("UPDATE fca01_group SET fca01_name = '" + newname + "' WHERE fca01_id = " + db_id);
        return 1;
    }
    
    public int delete(){
        SQLCon.exSQL("DELETE FROM fca01_group WHERE fca01_id = " + db_id);
        return 1;
    }
    
    public static int create(int parent, String name){
        String parentIns = "" + parent;
        if(parent == -1) parentIns = "NULL";
        SQLCon.exSQL("INSERT INTO fca01_group (fca01_fca01_id, fca01_name) VALUES (" + parentIns + ",'" + name + "')");
        return 1;
    }
    
    public int getDirectoryCount(){
        ResultSet rs = SQLCon.exSQLres("SELECT COUNT(*) AS count FROM fca04_directory WHERE fca01_id = " + db_id);
        try {
            rs.next();
            int pocet_slozek = rs.getInt("count");
            return pocet_slozek;
        } catch(SQLException ex) { }
        return 0;
         
    }

    public int getNumberOfSubGroups(){
        ResultSet rs = SQLCon.exSQLres("SELECT COUNT(*) AS pocet FROM fca01_group WHERE fca01_fca01_id = " + db_id);
        try {
            rs.next();
            int ret = rs.getInt("pocet");
            return ret;
        } catch(SQLException ex) { }
        return -1;
    }    
    
    public long getDataAmount(){
        long velikost = 0;
        ResultSet rs = SQLCon.exSQLres("SELECT SUM(fca03_picture.fca03_filesize) AS size FROM fca03_picture LEFT JOIN fca04_directory ON fca03_picture.fca04_id = fca04_directory.fca04_id WHERE fca04_directory.fca01_id = " + db_id);
        try {
            rs.next();
            velikost = rs.getLong("size");
        } catch(SQLException ex) { }
        return velikost;
    }    
    
    public static Vector getList(int parent){
        ResultSet rs;
        Vector ret = new Vector(10);
        String condition = "";
        if(parent == -1){
            condition = " WHERE fca01.fca01_fca01_id IS NULL";
        } else {
            condition = " WHERE fca01.fca01_fca01_id = " + parent;
        }
        String sql = MAIN_SQL + condition + " ORDER BY fca01.fca01_name";
        rs = SQLCon.exSQLres(sql);
        
        try {
            while(rs.next()){
                fcaGroup fcag = new fcaGroup();
                if(fcag.dbLoad(rs) == 1) ret.add(fcag);
            }
        } catch(SQLException ex) { }        
        
        return ret;
    }
}
